﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Test_Drag_N_drop
{
    public class BlockDnD : ConstructBlock
    {   
        private int offsetX = 0;
        private int offsetY = 0;
        private bool dragging = false;
        private bool over = false;

        private Texture2D textureDragging;
        
        public BlockDnD (int id) : base(id)
        {

        }

        public override void Initialize(GraphicsDevice graphic, int x, int y, TypeOfBlock type)
        {
            base.Initialize(graphic, x, y, type);
            dragging = true;
        }


        public override void LoadContent(ContentManager content)
        {
            base.LoadContent(content);
            textureDragging = content.Load<Texture2D>("Dragging");
            offsetX = _texture.Width / 2;
            offsetY = _texture.Height / 2;
            switch (type)
            {
                case TypeOfBlock.UP:
                    {
                        text = content.Load<Texture2D>("up");
                        break;
                    }

                case TypeOfBlock.DOWN:
                    {
                        text = content.Load<Texture2D>("down");
                        break;
                    }

                case TypeOfBlock.LEFT:
                    {
                        text = content.Load<Texture2D>("left");
                        break;
                    }

                case TypeOfBlock.RIGHT:
                    {
                        text = content.Load<Texture2D>("right");
                        break;
                    }

                case TypeOfBlock.END:
                    {
                        text = content.Load<Texture2D>("end");
                        break;
                    }
            }
        }


        public override void Update(GameTime gameTime, MouseState ms, MouseState pms)
        {
            if (rectangle.Contains(ms.X, ms.Y))
            {
                over = true;
                ConstructZone.getInstance().stopUpdate();
                if (ms.LeftButton == ButtonState.Pressed)
                {
                    if (pms.LeftButton == ButtonState.Released)
                    {
                        dragging = true;
                        ConstructZone.getInstance().newDraggingBlock(this);
                        offsetX = ms.X - (int)rectangle.X;
                        offsetY = ms.Y - (int) rectangle.Y;
                    }
                }
                else
                {
                    dragging = false;
                    ConstructZone.getInstance().clearDraggingBlock();
                }          
            }
            else
            {
                over = false;
            }

            if (dragging)
            {
                _texture = textureDragging;
                rectangle.X = ms.X - offsetX;
                rectangle.Y = ms.Y - offsetY;

                if(ResultZone.getInstance().isIn(rectangle))
                {
                    rectangle.X = ConstructZone.getInstance().Box.X  ;
                }

                List<ConstructBlock> myList = ConstructZone.getInstance().getList();
                foreach (ConstructBlock sprite in myList)
                {   

                    if (sprite.ID != this.myID && this.rectangle.Intersects(sprite.Rect) && this.son != sprite)
                    {
                        sprite.isFatherOf(this);
                    }
                }
            }

            else
            {
                if (BlockPoolZone.isIn(rectangle))
                {
                    ConstructZone.getInstance().shouldClearBlock(myID);
                }

                if (over)
                {
                    if (myID == -1)
                    {
                        ConstructZone.getInstance().addBlock(this);
                    }
                    _texture = textureOver;
                }
                else
                {
                   _texture = textureIdle;

                }

            }

            if (son == null)
            {
                haveSon = false;
            }
        }

    }
}
